package com.example.finalwork;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.UserDAO;
import com.example.finalwork.lmpl.UserDAOlmpl;
import com.example.finalwork.Entity.User;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "RegisterActivity";
    private EditText NewUserNameText,NewPasswordText,PasswordconfirmText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        findViewById(R.id.ConfirmChange_button).setOnClickListener(this);
        findViewById(R.id.Exit2_button).setOnClickListener(this);
        NewUserNameText = findViewById(R.id.oldpassword);
        NewPasswordText = findViewById(R.id.NewPassword);
        PasswordconfirmText = findViewById(R.id.ConfirmPassword);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ConfirmChange_button:
                if(!NewPasswordText.getText().toString().equals(PasswordconfirmText.getText().toString()))
                    Toast.makeText(this,"两次输入的密码不相同",Toast.LENGTH_SHORT).show();
                else if(NewUserNameText.getText().toString().equals("")){
                    Toast.makeText(this,"请输入您注册的用户名！",Toast.LENGTH_SHORT).show();
                    NewUserNameText.requestFocus();
                }
                else
                {
                    UserDAO userDAO = new UserDAOlmpl(this);
                    List<User> users = userDAO.GetAllUser();
                    for(int i = 0 ; i < users.size() ; i++)
                    {
                        User olduser = users.get(i);
                        if(olduser.getUserName().equals(NewUserNameText.getText().toString()))
                        {
                            Toast.makeText(this,"用户名已存在，请重新输入！",Toast.LENGTH_SHORT).show();
                            NewUserNameText.requestFocus();
                            return;
                        }
                    }
                    User user = new User();
                    user.setUserName(NewUserNameText.getText().toString());
                    user.setPassWord(NewPasswordText.getText().toString());
                    long result = userDAO.addUser(user);
                    Log.d(TAG,"add user result - >" + result);
                    assertNotEquals(-1,result);
                    Toast.makeText(this,"注册成功！",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(this,LoginActivity.class));
                }
                break;
            case R.id.Exit2_button:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,LoginActivity.class));
                break;
        }
    }

}
